<template>
  <div>

    <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
      <!-- 姓名 -->
      <el-form-item prop="username" label="姓名">
        <el-input v-model="formData.username" placeholder="请输入姓名" />
      </el-form-item>
      <!-- 手机 -->
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="formData.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <!-- 入职时间 -->
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="请选择入职日期" style="width: 100%;" />
      </el-form-item>
      <!-- 聘用形式 -->
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择活动区域">
          <el-option v-for="item in hireType" :key="item.id" prop="username" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <!-- 工号 -->
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="formData.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <!-- 部门 -->
      <el-form-item prop="departmentName" label="部门">
        <el-input
          v-model="formData.departmentName"
          placeholder="请选择部门"
          @focus="focusFn"
        />
        <el-tree
          v-show="treeShow"
          :data="treeData"
          :props="{label:'name'}"
          :default-expand-all="true"
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <!-- 转正时间 -->
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" type="date" placeholder="请选择转正日期" style="width: 100%;" />
      </el-form-item>
      <!-- 操作按钮 -->
      <el-form-item style="text-align:center;">
        <el-form-item size="large">
          <el-button type="primary" @click="addSubmit">添加</el-button>
          <el-button @click="addCancel">取消</el-button>
        </el-form-item>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 枚举
import EmployeeEnum from '@/constant/employees'
// 部门列表接口
import { getDepartmentsAPI } from '@/api/departments'
// 新增员工接口
import { addEmployeeAPI } from '@/api/employees'
// 扁平数据转化树形数据工具
import { transTreeDataFn } from '@/utils'

// 时间处理工具
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
export default {
  name: 'EmpDialog',
  data() {
    return {
      // 添加表单字段
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 添加表单的校验字段
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      hireType: EmployeeEnum.hireType, // 聘用形式枚举列表
      treeData: [], // 部门列表数据
      treeShow: false // 树形控件状态
    }
  },

  methods: {
    // 部门输入框聚焦
    focusFn() {
      this.getDepartmentsFn()
      this.treeShow = true
    },

    //   树形组件选择部门
    handleNodeClick(data) {
      if (data && data.children) return
      this.formData.departmentName = data.name
      this.treeShow = false
    },

    // 点击添加按钮
    addSubmit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        this.timeFormat()
        await addEmployeeAPI(this.formData)
        this.$refs.form.resetFields()
        this.$emit('close', '添加')
      })
    },

    // 点击取消按钮
    addCancel() {
      this.$refs.form.resetFields()
      this.$emit('close', '取消')
    },

    // 专门给父组件定义的方法,关闭后表单重置
    resetForm() {
      this.$refs.form.resetFields()
    },

    // 获取部门列表方法
    async getDepartmentsFn() {
      const res = await getDepartmentsAPI()
      console.log(res)
      this.treeData = transTreeDataFn(res.data.depts, '')
    },
    // 时间处理函数
    timeFormat() {
      this.formData.timeOfEntry = dayjs(this.formData.timeOfEntry).format('YYYY-MM-DD')
      this.formData.correctionTime = dayjs(this.formData.correctionTime).format('YYYY-MM-DD')
    }
  }

}
</script>

<style lang="scss" scoped>
.el-select{
    width: 100%;
}
.el-form{
    padding: 0 60px 0 20px;
}
</style>
